package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.my.util.MyUtil;

/**
 * Created by 528787067@qq.com on 2017/3/21.
 */

public class TeamCircleCommentBar extends Dialog implements View.OnClickListener {

    private Object tag;
    private Context context;
    private View contentView;
    private EditText commentEdit;
    private TextView commentBtn;

    public TeamCircleCommentBar(Context context){
        super(context, R.style.bg_loading_dialog);
        this.context = context;
        this.contentView = View.inflate(context, R.layout.team_circle_comment_bar, null);
        this.commentEdit = (EditText) contentView.findViewById(R.id.team_circle_comment_et);
        this.commentBtn = (TextView) contentView.findViewById(R.id.team_circle_comment_bn);
        this.commentBtn.setOnClickListener(this);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                clearInput();
                if(onBottomBarListener != null)
                    onBottomBarListener.onDismiss();
            }
        });
        setContentView(contentView);
        getWindow().setGravity( Gravity.BOTTOM);
        getWindow().getAttributes().width = context.getResources().getDisplayMetrics().widthPixels;
    }

    public void show() {
        clearInput();
        super.show();
        commentEdit.postDelayed(new Runnable() {
            @Override
            public void run() {
                MyUtil.startKeyword(context, commentEdit);
            }
        }, 300);
    }

    @Override
    public void dismiss() {
        MyUtil.closeKeyword(context, commentEdit);
        commentEdit.postDelayed(new Runnable() {
            @Override
            public void run() {
                TeamCircleCommentBar.super.dismiss();
            }
        }, 300);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.team_circle_comment_bn){
            if(commentEdit.getText().toString().trim().length() == 0){
                ToasUtil.toast(context, "请输入有效的内容后再提交");
            } else{
                if(onBottomBarListener != null)
                    onBottomBarListener.onCommentCommit(commentEdit.getText().toString());
                dismiss();
            }
        }
    }

    public int getPositionY(){
        int[] position = new int[2];
        contentView.getLocationOnScreen(position);
        return position[1];
    }

    public void clearInput(){
        commentEdit.setText("");
    }

    public void setHint(String hint){
        commentEdit.setHint(hint);
    }

    public void setText(String text){
        commentEdit.setText(text);
    }

    public Object getTag() {
        return tag;
    }

    public void setTag(Object tag) {
        this.tag = tag;
    }

    private OnBottomBarListener onBottomBarListener;
    public void setOnBottomBarListener(OnBottomBarListener bottomBarListener){
        this.onBottomBarListener = bottomBarListener;
    }
    public interface OnBottomBarListener{
        void onDismiss();
        void onCommentCommit(String content);
    }
}
